#include "passwddialog.h"

#include <QRegExpValidator>



PasswdDialog::PasswdDialog(QWidget *parent) : QWidget(parent)
{
    QString bgColor = "#FFFFFF";    //背景颜色
    QString borderColor = "#A6B5B8";//边框颜色

    pngLabel = new QLabel;
    pngLabel->setAlignment(Qt::AlignCenter);
    pngLabel->setPixmap(QPixmap(":/Img/closeeye.png").scaled(20, 20));

    lineEdit = new QLineEdit;
    lineEdit->setObjectName("lineEdit");
    lineEdit->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);

    lineEdit->setEchoMode(QLineEdit::Password);
    pngLabel->installEventFilter(this);

    QFrame *frame = new QFrame;
    frame->setObjectName("framePassword");
    lineEdit->setMaxLength(15);      // 限制只能输入15位

    //将控件按照横向布局排列
    QHBoxLayout *layout = new QHBoxLayout(frame);
    layout->setMargin(0);
    layout->setSpacing(0);
    layout->addWidget(lineEdit);
    layout->addWidget(pngLabel);

    QVBoxLayout *verticalLayout = new QVBoxLayout(this);
    verticalLayout->setMargin(0);
    verticalLayout->setSpacing(0);
    verticalLayout->addWidget(frame);

    connect(lineEdit, &QLineEdit::editingFinished, this, &PasswdDialog::alterEidt);
}

QString PasswdDialog::text() const
{
    return lineEdit->text();
}

QString PasswdDialog::setText(QString s)
{
    lineEdit->setText(s);
}

void PasswdDialog::clear()
{
    lineEdit->clear();
}

bool PasswdDialog::eventFilter(QObject *watched, QEvent *event)
{
    switch (event->type()) {
        case QEvent::Enter:
            lineEdit->setEchoMode(QLineEdit::Normal);  //显示密码
            pngLabel->setPixmap(QPixmap(":/Img/eye.png").scaled(20, 20));
            break;
        case QEvent::Leave:
            lineEdit->setEchoMode(QLineEdit::Password);//隐藏密码
            pngLabel->setPixmap(QPixmap(":/Img/closeeye.png").scaled(20, 20));
            break;
        default:
            break;
    }
    return QWidget::eventFilter(watched, event);
}
